package com.ys.erp.stage_config_service.service;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ys.erp.common.enums.EnableEnum;
import com.ys.erp.common.util.MoneyUtil;
import com.ys.erp.entity.data.bean.Information;
import com.ys.erp.entity.data.bean.MallSetting;
import com.ys.erp.entity.data.bean.PulseSetting;
import com.ys.erp.entity.service.IEventLogService;
import com.ys.erp.entity.service.IInformationService;
import com.ys.erp.entity.service.IMallSettingService;
import com.ys.erp.entity.service.IPulseSettingService;
import com.ys.erp.framework.bean.dto.StageEmpDTO;
import com.ys.erp.framework.exception.YsException;
import com.ys.erp.stage_config_service.bean.condition.ConfigCondition;
import com.ys.erp.stage_config_service.bean.condition.MallSettingCondition;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

/**
 * @author lwj
 * @date 2025/5/28
 */
@Service
@RequiredArgsConstructor
@Slf4j
@RefreshScope
public class ConfigService {

    private final IInformationService iInformationService;

    private final IEventLogService iEventLogService;

    private final IPulseSettingService iPulseSettingService;

    private final IMallSettingService iMallSettingService;

    /**
     * 配置购买须知
     *
     * @param empDto            登录员工
     * @param con               请求对象
     */
    public void setBuyInformation(StageEmpDTO empDto, ConfigCondition.SetBuyInformationCon con) {
        Information mallInformation = new Information();
        mallInformation.setContent(con.getBuyInformation());
        mallInformation.setEmpId(empDto.getId());
        mallInformation.setEmpName(empDto.getRealName());
        mallInformation.setInformationType(Information.Enums.MALL.name());
        mallInformation.setCreateTime(LocalDateTime.now());
        mallInformation.setUpdateTime(LocalDateTime.now());
        iInformationService.save(mallInformation);
    }

    /**
     * 配置幺币幺豆抵扣比列
     *
     * @param empDto                登录员工
     * @param con                   请求对象
     */
    public void setYsPulse(StageEmpDTO empDto, ConfigCondition.SetYsPulseCon con) {
        PulseSetting setting = new PulseSetting();
        Long ysMoneyL = MoneyUtil.str2Long(con.getYsMoney());
        Long ysPulseMoneyL = MoneyUtil.str2Long(con.getYsPulse());
        BigDecimal ysMoney = new BigDecimal(ysMoneyL);
        BigDecimal ysPulse = new BigDecimal(ysPulseMoneyL);
        if (ysPulse.compareTo(BigDecimal.ZERO) == 0) {
            throw new YsException(-1, "抵扣金额不能为零");
        }
        BigDecimal ratio = ysPulse.divide(ysMoney, 2, RoundingMode.HALF_UP);
        setting.setUseRatio(ratio.toString());
        setting.setYsPulse(ysPulseMoneyL.intValue());
        setting.setYsMoney(ysMoneyL.intValue());
        setting.setEmpId(empDto.getId());
        setting.setEmpName(empDto.getRealName());
        iPulseSettingService.save(setting);
        iEventLogService.insertEvent(empDto, "设置幺豆等价比列", setting.getId(), EnableEnum.NO.getEnable(), JSONObject.toJSONString(con));
    }

    public void upMallSetting(MallSettingCondition.UpMallSettingCon con) {
        MallSetting byId = iMallSettingService.getBaseSetting();
        BeanUtils.copyProperties(con, byId);
        byId.setBanner(JSONArray.toJSONString(con.getBanner()));
        byId.setMyPetActiveImg(con.getMyPetActiveImg());
        iMallSettingService.updateById(byId);
    }
}
